package com.blovesoft.tokenup;

import android.os.Bundle;
import android.support.annotation.NonNull;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blovesoft.tokenup.ui.home.HomeFragment;
import com.blovesoft.tokenup.ui.mine.MineFragment;
import com.llkj.common.SupportFragment;
import com.llkj.common.base.BaseActivity;
import com.llkj.common.di.component.AppComponent;
import com.llkj.common.model.BaseEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.tv_hangqing)
    TextView tvHangqing;
    @BindView(R.id.tv_mock_trading)
    TextView tvMockTrading;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private int position, prePosition;
    private SupportFragment[] mFragments = new SupportFragment[4];
    private TextView[] mtvs = new TextView[4];

    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    public boolean useEventBus() {
        return false;
    }

    @Override
    public void initView() {
        mtvs = new TextView[]{tvHome, tvHangqing, tvMockTrading, tvMine};
        mtvs[0].setSelected(true);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void beforeInitView(Bundle savedInstanceState) {
        super.beforeInitView(savedInstanceState);
        if (savedInstanceState == null) {
            mFragments[0] = HomeFragment.newInstance();
            mFragments[1] = HomeFragment.newInstance();
            mFragments[2] = HomeFragment.newInstance();
            mFragments[3] = MineFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_content, 0,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2],
                    mFragments[3]);
        } else {
            mFragments[0] = findFragment(HomeFragment.class);
            mFragments[1] = findFragment(HomeFragment.class);
            mFragments[2] = findFragment(HomeFragment.class);
            mFragments[3] = findFragment(MineFragment.class);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventComing(BaseEvent baseEvent) {
        switch (baseEvent.getCode()) {
            default:
                break;
        }
    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @OnClick({R.id.rl_home, R.id.rl_hangqing, R.id.rl_mine, R.id.rl_mock_trading})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_home:
                position = 0;
                break;
            case R.id.rl_hangqing:
                position = 1;
                break;
            case R.id.rl_mock_trading:
                position = 2;
                break;
            case R.id.rl_mine:
                position = 3;
                break;
            default:
                break;
        }
        if (position != prePosition) {
            showHideFragment(mFragments[position], mFragments[prePosition]);
            mtvs[position].setSelected(true);
            mtvs[prePosition].setSelected(false);
            prePosition = position;
        }
    }

}
